package com.project_test.kun.travelling_1st.HomePage.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.project_test.kun.travelling_1st.Discovery.Discover_Detail_Activity;
import com.project_test.kun.travelling_1st.HomePage.Util.URL;
import com.project_test.kun.travelling_1st.HomePage.category.HomePListItemBanner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-10-19.
 */
public class HomePHeadPagerAdapter extends PagerAdapter {
    private List<HomePListItemBanner> data;//获取每个广告界面的信息
    private List<ImageView> viewlist;
    private Context mContext;

    public HomePHeadPagerAdapter(List<HomePListItemBanner> data, List<ImageView> viewlist, Context context) {
        this.data = data;
        this.viewlist = viewlist;
        mContext = context;
    }

    @Override
    public int getCount() {
        //设置成最大，使用户看不到边界
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0==arg1;
    }
    @Override
    public void destroyItem(ViewGroup container, int position,
                            Object object) {
        //Warning：不要在这里调用removeView
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //对ViewPager页号求模取出View列表中要显示的项
        if (viewlist.size()!=0)
        position %=viewlist.size();
        if (position<0)
            position = viewlist.size()+position;

        ImageView view = viewlist.get(position);
        final int index=position;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,Discover_Detail_Activity.class);
                String detailPath= URL.DETAIL_PATH_1+data.get(index%data.size()).getItem_id()+URL.DETAIL_PATH_2;
                intent.putExtra("detailPath",detailPath);
                mContext.startActivity(intent);
            }
        });


        //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
        ViewParent vp =view.getParent();
        if (vp!=null){
            ViewGroup parent = (ViewGroup)vp;
            parent.removeView(view);
        }
        container.addView(view);
        //add listeners here if necessary
        return view;
    }
}